package com.example.demo.controller;

import com.example.demo.pojo.*;
import com.example.demo.service.BooksService;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


/***
 * Books - Controller
 */
@RestController
@CrossOrigin
@RequestMapping("/books")
public class BooksController {

    @Autowired
    private BooksService booksService;

    private Logger logger = LoggerFactory.getLogger(BooksController.class);


    @PostMapping("/saveStock")
    public Result saveStock(Stock stock){
        // 库存编号规范： S00001
        return this.booksService.saveStock(stock);
    }


    /**
     * 入库操作
     * @param books
     * @return
     */
    @PostMapping("/saveBooks")
    public Result saveBooks(Stock stock,Books books, HttpSession session){
        //图书编号规范 B00001
        return this.booksService.saveBook(stock,books);
    }

    /**
     * 图书信息修改
     * @param books
     * @return
     */
    @PatchMapping("/saveBooks")
    public Result updateBools(Books books){
        logger.info("books Id:" + books.getBook_id());
        logger.info("books Id:" + books.getPub_date());
        Result result = this.booksService.updateBookById(books);
        return result;
    }

    
    /**
     * 根据Id图书信息
     * @param book_id
     * @return
     */
    @GetMapping("/editBooks/{book_id}")
    public Result selBooksById(@PathVariable int book_id){
        return this.booksService.selBookAndStockByBookId(book_id);
    }

    /**
     * 图书出库
     * @param book_id
     * @return result
     */
    @PatchMapping("/outBoundBooks/{book_id}")
    public Result outBoundBooksById(@PathVariable int book_id,HttpSession session){
        TokenData tokenData = (TokenData)session.getAttribute("currentUser");
        if(tokenData.getUser_state() == 0 || tokenData.getUser_state() == 1){ //管理员 或者是超管
            return  this.booksService.outBoundBooksById(book_id);
        }
        return ResultFactory.failure("读者没有此权限");
    }


    /**
     * 添加图书分类
     * @param classInfo
     * @return
     */
    @GetMapping("/saveClassInfo")
    public Result saveClassInfo(ClassInfo classInfo){
        Result result = this.booksService.saveClassInfo(classInfo);
        return  result;

    }


    /**
     * 查询所有的分类信息
     * @return
     */
    @GetMapping("/selAllClassInfo")
    public List<ClassInfo> selAllClassInfo(){
        return  this.booksService.selAllClassInfo();
    }

    /**
     * 图书暂停借阅
     */
    @PatchMapping("/stopLendBookById/{book_id}")
    public Result stopLendBookById(@PathVariable int book_id){

        Result result = this.booksService.upateBookState(book_id);
        if(result == null){
            result = ResultFactory.failureWithStatus(999,"修改图书为暂停借阅失败");
        }
        return  result;

    }

    /**
     * 图书转移
     */
    @PatchMapping("/moveBookClassByBookId/{book_id}")
    public Result moveBookClassByBookId(@PathVariable int book_id,ClassInfo classinfo){

        return this.booksService.updateBookClassByBookId(book_id,classinfo.getClass_id());
    }

    /**
     * 图书推荐
     */
    @GetMapping("/recommentBook")
    public Result recommentBook(){
        List<Books> books = this.booksService.recommentBook();
        return ResultFactory.successWithData(books);
    }

    /**
     * 图书列表分页展示
     */
    @GetMapping("/bookItemPage")
    public Result booksItemPage(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize){
        PageBean<Books> booksPageBean = this.booksService.testPage(currentPage, pageSize);
        return ResultFactory.successWithStatusAndData(200,"分页内容",booksPageBean);
    }

    /**
     * 图书查询
     */
    @GetMapping("/queryBooksInfoByConditions")
    public Result queryBooksInfoByConditions(Books books,@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize){
        PageBean<Books> booksPageBean = this.booksService.queryBookInfoByConditions(books, currentPage, pageSize);
        if(booksPageBean.getTotalNum() == 0){
            return ResultFactory.failureWithStatus(999,"没有相应的图书信息");
        }
        return ResultFactory.successWithStatusAndData(200,"查找成功",booksPageBean);
    }

    /**
     * 根据库存编号查询对应的图书
     */
    @GetMapping("/queryBoosInfoByStockNo/{stock_no}")
    public Result queryBoosInfoByStockNo(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize,@PathVariable  String stock_no){
        return this.booksService.queryBoosInfoByStockNo(stock_no,currentPage,pageSize);
    }

    /**
     * 出库表列表展示
     */
    @GetMapping("/outBoundItemPage")
    public Result outBoundItemPage(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize){
        return this.booksService.queryOutBoundList(currentPage,pageSize);
    }

}
